package com.naiterui.ehp.bs.esign.controller.inner;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.bo.esign.DigitalCertificateBO;
import com.naiterui.ehp.bp.bo.esign.EsignDoctorBO;
import com.naiterui.ehp.bp.bo.esign.EsignNoSecretBO;
import com.naiterui.ehp.bp.bo.esign.NoSecretParamBO;
import com.naiterui.ehp.bp.bo.esign.PreSignResultBO;
import com.naiterui.ehp.bp.bo.esign.WillTypeBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bs.esign.api.bjca.vo.SignUserParamVO;
import com.naiterui.ehp.bs.esign.exception.ExceptionCodes;
import com.naiterui.ehp.bs.esign.service.IDoctorService;

/**
 * @author guoyongxiang
 * @date 2019/12/3 16:31
 * @since 1.0.0
 */
@RequestMapping("esign/inner/dr")
@RestController
public class EsignInnerDrController {

    private static final Logger LOGGER = LoggerFactory.getLogger(EsignInnerDrController.class);

    @Autowired
    private IDoctorService doctorService;

    /**
     * 注册电子签名用户-医生
     *
     * @param signUserParamVO
     *
     * @return
     *
     * @throws BusinessException
     * @throws IOException
     */
    @PostMapping("esign/doctor/add")
    public ResponseVO<Void> eSignUserAdd(SignUserParamVO signUserParamVO) throws BusinessException {
        LOGGER.info("请求创建电子签名医师用户 doctorId:{}", signUserParamVO.getUserId());

        doctorService.addUserDr(signUserParamVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 更新电子签名手机号
     *
     *
     * @return
     *
     * @throws BusinessException
     * @throws IOException
     */
    @PostMapping("esign/doctor/update/phone")
    public ResponseVO<Void> eSignUserAdd(Long doctorId, String phone) throws BusinessException {
        LOGGER.info("请求更新电子签名手机号 doctorId:{}, phone:{}", doctorId, phone);

        doctorService.updatePhone(doctorId, phone);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * <pre>
     *     发起实名认证
     * </pre>
     *
     * @param name   姓名
     * @param idCard 身份证号
     * @param phone  手机号
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("esign/realname/code")
    public ResponseVO<Void> realnameCode(String name, String phone, String idCard) throws BusinessException {
        LOGGER.info("发起短信实名认证 phone:{}", phone);
        doctorService.getRealnameCode(name, phone, idCard);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * <pre>
     *     实名认证校验
     * </pre>
     *
     * @param phone 手机号
     * @param code  验证码
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("esign/realname/check")
    public ResponseVO<Boolean> readlnameCheck(String phone, String code) throws BusinessException {
        LOGGER.info("发起短信实名认证 phone:{}", phone);
        return PublicService.returnResponseVO(doctorService.checkRealname(phone, code));
    }

    /**
     * 获取医生签名账号信息
     *
     * @param doctorId
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("esign/info")
    public ResponseVO<EsignDoctorBO> getEsignInfo(Long doctorId) throws BusinessException {
        LOGGER.info("获取医生签名账号信息：doctorId={}", doctorId);
        EsignDoctorBO bo = doctorService.getEsignInfo(doctorId);
        return PublicService.returnResponseVO(bo);
    }

    /**
     * 获取意愿签署方式列表
     * @return
     * @throws BusinessException
     */
    @PostMapping("will/types")
    public ResponseVO<List<WillTypeBO>> getWillTypes(Long doctorId) throws BusinessException {
        LOGGER.info("医师获取意愿签署方式列表：doctorId={}", doctorId);
        return PublicService.returnResponseVO(doctorService.getWillTypes(doctorId));
    }

    /**
     * 更新意愿签署方式
     *
     * @param doctorId
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("/will/type/update")
    public ResponseVO<Void> updateWillType(Long doctorId, String willType) throws BusinessException {
        LOGGER.info("更新签名方式：doctorId={}, willType={}", doctorId, willType);
        doctorService.updateWillType(doctorId, willType);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 更新签名密码
     *
     * @param doctorId
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("/password/update")
    public ResponseVO<Void> updateSignPassword(Long doctorId, String password) throws BusinessException {
        LOGGER.info("更新签名密码：doctorId={}, password={}", doctorId, password);
        doctorService.updateSignPassword(doctorId, password);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @GetMapping("/getNoSecret")
    public ResponseVO<Integer> getNoSecret(Long doctorId) throws BusinessException {
        LOGGER.info("获取免密状态：doctorId={}", doctorId);
        return PublicService.returnResponseVO(doctorService.getNoSecretByDoctorId(doctorId));
    }

    @GetMapping("/getNoSecretInfo")
    public ResponseVO<EsignNoSecretBO> getNoSecretInfo(Long doctorId) throws BusinessException {
        LOGGER.info("获取免密状态信息：doctorId={}", doctorId);
        return PublicService.returnResponseVO(doctorService.getEsignNoSecretByDoctorId(doctorId));
    }

    @PostMapping("/preNoSecret")
    public ResponseVO<PreSignResultBO> preNoSecret(Long doctorId) throws BusinessException {
        LOGGER.info("医生设置免密预签署：{}", doctorId);
        return PublicService.returnResponseVO(doctorService.preNoSecret(doctorId));
    }
    @PostMapping("/setNoSecret")
    public ResponseVO<Void> setNoSecret(@RequestBody NoSecretParamBO paramBO) throws BusinessException {
        LOGGER.info("医生设置免密：{}", JsonMapper.toJson(paramBO));
        // doctorService.setNoSecret(doctorId, noSecret, authPwd);
        doctorService.setNoSecret(paramBO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @GetMapping("/saveSealImg")
    public ResponseVO<Void> saveSealImg(Long doctorId, String sealImg) throws BusinessException {
        LOGGER.info("更新签名用户签章图片：doctorId={}, sealImg={}", doctorId, sealImg);
        doctorService.saveSealImg(doctorId, sealImg);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 更新医生esign认证状态
     * @param doctorId
     * @param status
     * @return
     * @throws BusinessException
     */
    @PostMapping("/setAuthStatus")
    public ResponseVO<Void> setAuthStatus(Long doctorId, Integer status) throws BusinessException {
        LOGGER.info("请求医生esign认证状态 doctorId:{}, status:{}", doctorId, status);
        doctorService.setAuthStatus(doctorId, status);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 获取用户是否注册过ca账户
     * @param number
     * @return
     * @throws BusinessException
     */
    @GetMapping("/getAccountByDoctorIdAndIdNo")
    public ResponseVO<Boolean> getAccountByDoctorIdAndIdNo(Long doctorId, String number) throws BusinessException {
        LOGGER.info("获取用户是否注册过ca账户 doctorId:{}, number:{}",doctorId, number);
        return PublicService.returnResponseVO(doctorService.getSignAccountByDoctorIdAndIdNo(doctorId, number));
    }

    /**
     * 获取医生数字证书
     * 
     * @param doctorId
     * @return
     * @throws BusinessException
     */
    @GetMapping("/getDigitalCertificate")
    public DigitalCertificateBO getDigitalCertificate(Long doctorId) throws BusinessException {
        return doctorService.getDigitalCertificate(doctorId);
    }

    /**
     * 根据身份证号和认证状态获取身份信息
     * @param idNo
     * @param authStatus
     * @return
     */
    @GetMapping("/getAccount")
    public EsignDoctorBO getAccountByIdNoAndAuthStatus(String idNo, Integer authStatus) {
        return doctorService.getAccountByIdNoAndAuthStatus(idNo, authStatus);
    }

    @GetMapping("/checkDoctorRealname")
    public ResponseVO<Void> checkDoctorRealname(Long doctorId, String idCard) throws BusinessException {
        LOGGER.info("检查该医生身份证是否已实名 doctorId: {}, idCard:{}", doctorId, idCard);
        doctorService.checkDoctorRealname(idCard);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }
}
